package com.briup.smartlabs.exception;

import com.briup.smartlabs.utils.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

public class GlobleExceptionHandler {
    @RestControllerAdvice
    public class GlobalExceptionHandler {
        // 标记要捕获哪些异常
        @ExceptionHandler(Exception.class)
        public Object handleException(Exception e) {
            Result result = null;
            if(e instanceof ServiceException) {
                result = Result.failure(((ServiceException) e).getResultCode(),"请检查项目日志找出异常");

            }else {
                // 这里我们希望给到的是能够自定义的code和msg
                // 只要是你写的代码有问题(出现异常)，前端拿到的状态码一定是500
                result = Result.customResultCode(500,e.getMessage());
            }

            return result;
        }
    }

}
